﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;

namespace ConnectDatabase
{
    public class GeoCityDB
    {
        DatabaseDataContext dbContext = new DatabaseDataContext();

        #region Các hàm cơ bản
        public List<tblGeoCity> GetAll()
        {
            try
            {
                var list = from a in dbContext.tblGeoCities where a.Visible select a;
                return list.ToList<tblGeoCity>();
            }
            catch
            {
                return new List<tblGeoCity>();
            }
        }

        public tblGeoCity GetByKey(string code)
        {
            try
            {
                var obj = dbContext.tblGeoCities.SingleOrDefault(a => a.Code == code && a.Visible);
                return obj;
            }
            catch
            {
                return null;
            }
        }

        public bool Insert(tblGeoCity obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblGeoCities.SingleOrDefault(a => a.Code == obj.Code && !a.Visible);
                if (exist != null)
                {
                    exist.CountryCode = obj.CountryCode;
                    exist.Name_Vi = obj.Name_Vi;
                    exist.Name_En = obj.Name_En;
                    exist.Name_Fr = obj.Name_Fr;
                    exist.Location = obj.Location;
                    exist.Visible = obj.Visible;
                    dbContext.SubmitChanges();
                    isOk = true;
                }
                else
                {
                    dbContext.tblGeoCities.InsertOnSubmit(obj);
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                return false;
            }
            return isOk;
        }

        public bool Update(tblGeoCity obj)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblGeoCities.SingleOrDefault(a => a.Code == obj.Code);
                if (exist != null)
                {
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }

        public bool Delete(string code)
        {
            bool isOk = false;
            try
            {
                var exist = dbContext.tblGeoCities.SingleOrDefault(a => a.Code == code);
                if (exist != null)
                {
                    exist.Visible = false;
                    dbContext.SubmitChanges();
                    isOk = true;
                }
            }
            catch
            {
                isOk = false;
            }
            return isOk;
        }
        #endregion

        public List<tblGeoCity> GetByCountryCode(string countryCode)
        {
            try
            {
                if (string.IsNullOrEmpty(countryCode))
                {
                    var list = from a in dbContext.tblGeoCities where a.Visible select a;
                    return list.ToList<tblGeoCity>();
                }
                else
                {
                    var list = from a in dbContext.tblGeoCities where a.CountryCode == countryCode && a.Visible select a;
                    return list.ToList<tblGeoCity>();
                }
            }
            catch
            {
                return new List<tblGeoCity>();
            }
        }
    }
}
